package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.ContactListAdapter;
import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.view.QuickAlphabeticBar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class UserContactsActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ListView contactListView;
	private QuickAlphabeticBar alpha;
	private LinearLayout addFriendLayout;
	/****************** 其它变量 **************************/
	private List<UserEntity> userContactList;
	private ContactListAdapter contactListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_contact);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取列表数据
		requestTopListData();

	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.contactListView = (ListView) findViewById(R.id.lv_user_contact_list);
		this.alpha = (QuickAlphabeticBar) findViewById(R.id.fast_scroller);
		this.addFriendLayout = (LinearLayout) findViewById(R.id.ll_common_right);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {

		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent contactsIntent = new Intent(
							UserContactsActivity.this, SettingActivity.class);

					startActivity(contactsIntent);
					break;
				case R.id.ll_common_right:
					Intent addFriendIntent = new Intent(
							UserContactsActivity.this,
							AddFriendSourceActivity.class);
					startActivity(addFriendIntent);
					break;

				default:
					break;
				}
			}
		};
		this.addFriendLayout.setOnClickListener(moreClick);
		this.contactListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				UserEntity people = (UserEntity) contactListAdapter
						.getItem(position);
				Intent businessIntent = new Intent(UserContactsActivity.this,
						UserChatActivity.class);
				businessIntent.putExtra("friendInfo", people);
				startActivity(businessIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	/**
	 * 获取附近用户列表数据
	 */
	private void requestTopListData() {
		if (commonUserInfo == null) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("userId",
								commonUserInfo.getUserId());
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow", "100");
						nearbyBusinessParamMap.put("sort", "1");
						nearbyBusinessParamMap.put("language", "1");
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								UserContactsActivity.this,
								Constants.USER_FRIEND_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							userContactList = new ParseJson()
									.parseUserFriendListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setPeopleListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用评论列表适配器
	 */
	public void setPeopleListAdapter() {
		if (CollectionUtil.UnNullAndZero(userContactList)) {
			this.contactListAdapter = new ContactListAdapter(this,
					userContactList, alpha);
			this.contactListView.setAdapter(contactListAdapter);
			this.alpha.init(UserContactsActivity.this);
			this.alpha.setListView(contactListView);
			this.alpha.setHight(alpha.getHeight());
			this.alpha.setVisibility(View.VISIBLE);
		}
	}
}